﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessMethod
    {
        #region Create
        public static void createMethod(Method m)
        {
            try
            {
                dataMethod.createMethod(m);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating method");
            }
        }

        public static void createMethod(String type, String description, String name, String path)
        {
            try
            {
                Method m = new Method();
                m.Type = type;
                m.Description = description;
                m.Name = name;
                m.Path = path;
                
                dataMethod.createMethod(m);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating method");
            }
        }
        #endregion

        #region Get
        public static Method getMethod(Guid guid)
        {
            try
            {
                return dataMethod.getMethod(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting method");
            }
        }

        public static List<Material> getMaterialByMethod(Method m)
        {
            try
            {
                List<Material> material = new List<Material>();

                Expression<Func<Methods_Material, bool>> whereClause = (Methods_Material m_m) => (m_m.MethodID == m.MethodID);
                List<Methods_Material> mm = dataMethod.getMethod_Material(whereClause).ToList();

                foreach (Methods_Material e in mm)
                {
                    Material ma = dataMaterial.getMaterial(e.MaterialID);

                    if (!material.Contains(ma))
                    {
                        material.Add(ma);
                    }
                }

                return material;
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting material by method");
            }
        }
        #endregion

        #region Set
        public static Boolean changeMethod(Method m)
        {
            try
            {
                return dataMethod.changeMethod(m);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing method");
            }
        }
        #endregion

        #region Delete
        #endregion
    }
}
